import csv
import json
import time

import jsonpath
import requests

text1 = []
for l in range(3):
    num = 4
    # url = "https://shuyang.anjuke.com/v3/ajax/map/sale/3792/prop_list/?room_num=-1&price_id=-1&area_id=-1&floor=-1&orientation=-1&is_two_years=0&is_school=0&is_metro=0&order_id=0&p=5&zoom=12&lat=34.041148_34.193691&lng=118.258498_119.362336&kw=&et=86b5d3&ib=1&bst=pem367"
    url = "https://shuyang.anjuke.com/v3/ajax/map/sale/3792/prop_list/"

    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36'
    }

    pg = {
        'room_num': -1,
        'price_id': -1,
        'area_id': -1,
        'floor': -1,
        'orientation': -1,
        'is_two_years': 0,
        'is_school': 0,
        'is_metro': 0,
        'order_id': 0,
        'p': '%s' % num,
        'zoom': 12,
        'lat': '34.041148_34.193691',
        'lng': '118.258498_119.362336',
        'kw': ' ',
        'et': '86b5d3',
        'ib': 1,
        'bst': 'pem367'
    }
    num += 1
    r = requests.get(url=url, headers=headers, params=pg)

    a = json.loads(r.text.encode('utf-8').decode('unicode_escape'))

    # print(a)
    # a = dict()
    ret = jsonpath.jsonpath(a, "$.val.props")
    # print(ret)
    i = 0
    # c = 0
    for j in range(60):
        for fc in ret:
            item = dict()
            item['region_name'] = jsonpath.jsonpath(fc, '$..region_name')[i]
            item['block_name'] = jsonpath.jsonpath(fc, '$..block_name')[i]
            item['rhval'] = jsonpath.jsonpath(fc, '$..rhval')[i]
            item['area'] = jsonpath.jsonpath(fc, '$..area')[i]
            item['price'] = jsonpath.jsonpath(fc, '$..price')[i]
            item['long_title'] = jsonpath.jsonpath(fc, '$..long_title')[i]
            item['comm_name'] = jsonpath.jsonpath(fc, '$..comm_name')[i]
            item['house_orient_name'] = jsonpath.jsonpath(fc, '$..house_orient_name')[i]
            item['floor_tag'] = jsonpath.jsonpath(fc, '$..floor_tag')[i]
            text = "%s %s %s %s %s %s %s %s %s" % (
                item['region_name'], item["block_name"], item['rhval'], item['area'], item['price'], item['long_title'],
                item['comm_name'], item['house_orient_name'], item['floor_tag'])
            text1.append(item)
            i += 1
            # c += 1
            # print(c)
            # text1 = text.split()
            # print(text1)
            # print(type(text1))
    with open('沭阳房产_20201106.csv', 'w', encoding='utf-8') as f:
        # 创建一个csv的DictWriter对象，这样才能够将写入csv格式数据到这个文件
        f_csv = csv.DictWriter(f, ["region_name", "block_name", 'rhval', 'area', 'price', 'long_title', 'comm_name',
                                   'house_orient_name', 'floor_tag'])
        # 写入一行（我们用第一行当做表头）
        f_csv.writeheader()
        # 写入多行行（当做数据）
        f_csv.writerows(text1)
        print('爬取成功')
# time.sleep(2)
